﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;

namespace MS.InformationManagement.BLL.DeliveryForms
{
    public class MarkDeliveriesAsSentCommand : IBaseCommand
    {

        #region Fields

        EntitySet<EnvíoDeCorrespondencia> _deliveries;
        EntitySet<EnvíoDeCorrespondencia> _deliveriesBackup;

        #endregion

        #region Constructors

        public MarkDeliveriesAsSentCommand(EntitySet<EnvíoDeCorrespondencia> deliveries)
        {
            _deliveries = deliveries;
            _deliveriesBackup = deliveries;
        }

        #endregion

        #region Implemented Methods

        public void Execute()
        {
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                foreach (var entity in _deliveries)
                {
                    dc.EnviosDeCorreo.Attach(entity);
                    entity.Enviado = true;
                    entity.FechaDeEnvío = DateTime.Now;
                }
                dc.SubmitChanges();
            }
        }

        public void Undo()
        {
            using (var dc = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                for (int i = 0; i < _deliveries.Count; i++)
                {
                    dc.EnviosDeCorreo.Attach(_deliveries[i]);
                    _deliveries[i].Enviado = _deliveriesBackup[i].Enviado;
                    _deliveries[i].FechaDeEnvío = _deliveriesBackup[i].FechaDeEnvío;
                }
                dc.SubmitChanges();
            }
        }

        #endregion


    }
}
